<?php
/**
 * 小新
 */
namespace app;

use app\lib\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\facade\Env;use think\Response;
use Throwable;

/**
 * 应用异常处理类
 */
class ExceptionHandle extends Handle
{

    //实现自定义异常 声明参数
    private $code;
    private $msg;
    private $errorCode;
    private $data;
    private $request_url;

    /**
     * 不需要记录信息（日志）的异常类列表
     * @var array
     */
    protected $ignoreReport = [
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        DataNotFoundException::class,
        ValidateException::class,
    ];

    /**
     * 记录异常信息（包括日志或者其它方式记录）
     *
     * @access public
     * @param  Throwable $exception
     * @return void
     */
    public function report(Throwable $exception): void
    {
        // 使用内置的方式记录异常日志
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */

    public function render($request, Throwable $e): Response
    {
        //dump();die;

        //单利模式进
        if ($e instanceof BaseException)
        {
            //如果是自定义异常，则控制http状态码，不需要记录日志
            //因为这些通常是因为客户端传递参数错误或者是用户请求造成的异常
            //不应当记录日志
            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->errorCode = $e->errorCode;
            $this->data = $e->data;
            $this->request_url = $e->request_url;
        }
        else{
            // 调试状态下需要显示TP默认的异常页面，因为TP的默认页面，很容易看出问题
            if(Env::get('app_debug')){
                return parent::render($request,$e);
            }

            // 如果是服务器未处理的异常，将http状态码设置为500，并记录日志
            $this->code = 500;
            $this->msg = 'sorry，we make a mistake. (^o^)Y';
            $this->errorCode = 999;
            //写入日记的方法
            //$this->recordErrorLog($e);
        }

        $result = [
            'msg' => $this->msg,
            'errorCode' => $this->errorCode,
            'data' => $this->data,
            'request_url' => $this->request_url
        ];

        return json($result,$this->code);

        //return parent::render($request, $e);
    }
}
